import Promise from '../../../../utils/es6-promise/es6-promise'
import regeneratorRuntime from '../../../../utils/regenerator-runtime/runtime'
var WxParse = require('../../../../utils/wxParse/wxParse.js');
const utils = require('../../../../utils/util.js');
const app=getApp();
Page({
  data: {
    getDetailUrl:"/api/SeniorService/seniorDetail",
    url:"/api/SeniorService/checkInvite",
    info:null,
    token:null,
    loading:false,
    successColor:"#0aba05",
    errorColor:"#f7625f",
    codeType:0,
    code:"",
    isSumbit:false,
    jumpUrl:"../submit/index",
    iconSize: [20],
    iconColor: [
      '#f7625f', '#0aba05'
    ],
    iconType: [
      'success', 'cancel'
    ],
    isHide:false
  },
  onShow:function(){
    var isHide=this.data.isHide;
    this.setData({
      isHide:false,
      loading:false,
      codeType:0,
      isSumbit:false,
      code:""
    })
    if(isHide) this.onLoad();
  },
  onHide:function(){
    this.setData({
      isHide:true
    })
  },
  formSubmit: async function(e){
    const params = e.detail.value;
    params.formId = e.detail.formId;
    params.token = this.data.token;
    this.setData({
      loading:true
    })
    var result=await this._checkMa(params);
    if(result.state){
      this.setData({codeType:2})
      var msg=result.msg;
      /* wx.showModal({
        title:msg,
        showCancel:false,
        complete: data=>{
          wx.navigateTo({
            url: this.data.jumpUrl+"?code="+params.code
          })
        }
      }) */
      wx.navigateTo({
        url: this.data.jumpUrl+"?code="+params.code
      })
    }
    else{
      this.setData({codeType:1,loading:false})
      /* wx.showToast({
        title:result.msg,
        icon:"none",
        duration:1000
      }) */
    }
  },
  codeInput:function(e){
    var code=e.detail.value;
    this.setData({
      code:code,
      isSumbit:!!code
    })
  },
  onLoad: async function (params) {
    console.log("onLoad");
    wx.showLoading({
      title:"加载中..."
    })
    const token = await app.getToken();
    this.setData({
      token:token
    })
    var result=await this._loadData();
    wx.hideLoading();
    if(result.state){
      var info=result.info;
      this.setData({
        info:info
      })
      /**
      * WxParse.wxParse(bindName , type, data, target,imagePadding)
      * 1.bindName绑定的数据名(必填)
      * 2.type可以为html或者md(必填)
      * 3.data为传入的具体数据(必填)
      * 4.target为Page对象,一般为this(必填)
      * 5.imagePadding为当图片自适应是左右的单一padding(默认为0,可选)
      */
      var article = this.data.info;
      article=this.escape2Html(article);
      article=article.replace(/\<img src=\"/g,'<img class="image" src="'+app.globalData.basePath);
      article=article.replace(/\<p>/g,'<p class="p">');
      this.setData({
        article:article,
        loading:false
      })
    }
    else{  
      wx.showToast({
        title:result.msg,
        icon:"none",
        duration:1000
      })
    }
  },
  escape2Html: function (str) {
    var arrEntities = { 'lt': '<', 'gt': '>', 'nbsp': ' ', 'amp': '&', 'quot': '"' };
    return str.replace(/&(lt|gt|nbsp|amp|quot);/ig, function (all, t) { return arrEntities[t]; });
  },
  _loadData:function(){
    var p = new Promise((resolve,reject)=>{
        //做一些异步操作
        app.request(this.data.getDetailUrl,{token:this.data.token},res=>{
          //console.log(res);
          var data=res.data;
          if(data.state){
            //resolve({state:true,info:JSON.parse(data.info)});
            resolve({state:true,info:data.data});
          }
          else{
            resolve({state:false,msg:data.msg});          
          }
        },error=>{
          resolve({state:false,msg:"获取失败，请重试"});
        })
    });
    return p;
  },
  _checkMa:function(params){
    var p = new Promise((resolve,reject)=>{
      //做一些异步操作
      app.request(this.data.url,{token:this.data.token,invite_code:params.code},res=>{
        var data=res.data;
        if(data){
          resolve({state:true,msg:"验证成功"});
        }
        else{
          resolve({state:false,msg:"验证失败"});          
        }
      },error=>{
        resolve({state:false,msg:"验证失败"});
      })
  });
  return p;
    
  }
})